package com.mars.tukuan.web.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mars.base.common.base.BaseController;
import com.mars.base.common.base.BaseResult;
import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.Paginator;
import com.mars.tukuan.dto.CarVo;
import com.mars.tukuan.entity.Car;
import com.mars.tukuan.rpc.api.CarService;
import com.mars.tukuan.utils.file.FileMeta;
import com.mars.tukuan.utils.file.FileUtil;
import com.mars.tukuan.utils.file.cloud.UploadCloudStroe;
import com.mars.tukuan.utils.file.cloud.UploadResult;

@RestController
@RequestMapping("/api/tukuan/mobile/car/manager")
public class CarController extends BaseController {

	@Autowired
	private CarService service;
	
	@Autowired
	private UploadCloudStroe uploadCloudStroe;

	@PostMapping(value = "/add")
	public Object add(@RequestBody Car entity) {
		if (null != this.service.checkValue("car_num", entity.getCarNum())) {
			return BaseResult.build(false, String.format("该车牌号码【%s】已存在", entity.getCarNum()));
		}
		try {
			this.service.insertSelective(entity);
		} catch (Exception e) {
			return BaseResult.addFailed();
		}
		return BaseResult.addOk();
	}

	@GetMapping(value = "/delete/{ids}")
	public Object delete(@PathVariable String ids) {
		try {
			this.service.deleteByIds(ids);
		} catch (Exception e) {
			return BaseResult.deleteFailed();
		}
		return BaseResult.deleteOk();
	}

	@PostMapping(value = "/update")
	public Object update(@RequestBody Car entity) {
		try {
			Car oldCar = this.service.selectById(entity.getId()) ;
			if(null != oldCar.getCarNum() && !oldCar.getCarNum().equals(entity.getCarNum())) {
				if(null != this.service.checkValue("car_num", entity.getCarNum())) {
					return BaseResult.build(false, String.format("该车牌号码【%s】已存在", entity.getCarNum()));
				}
			}
			this.service.updateSelective(entity);
		} catch (Exception e) {
			return BaseResult.updateFailed();
		}
		return BaseResult.updateOk();
	}

	@GetMapping(value = "/get/{id}")
	public Car get(@PathVariable Long id) {
		return this.service.selectById(id);
	}
	
	@GetMapping(value = "/list")
	public Object list(Car entity) {
		try {
			return  this.service.selectPage(new Paginator(request), entity) ;
		} catch (AppException e) {
			return BaseResult.loadFailed();
		}
	}
	
	@GetMapping(value = "/listByCarState")
	public Object listByCarState(CarVo entity) {
		try {
			return  this.service.selectPageByCarState(new Paginator(request), entity) ;
		} catch (AppException e) {
			return BaseResult.loadFailed();
		}
	}
	
	@GetMapping(value = "/listByCarCheck")
	public Object listByCarCheck(CarVo entity) {
		try {
			return  this.service.selectPageByCarCheck(new Paginator(request), entity) ;
		} catch (AppException e) {
			return BaseResult.loadFailed();
		}
	}
	
	/**
	 * 我的上传
	 * @param entity
	 * @return
	 */
	@GetMapping(value = "/myCarStateList")
	public Object myCarStateList(CarVo entity) {
		try {
			return  this.service.myCarState(new Paginator(request), entity) ;
		} catch (AppException e) {
			return BaseResult.loadFailed();
		}
	}
	
	@GetMapping(value = "/selectPageByHarm")
	public Object selectPageByHarm(CarVo entity) {
		try {
			return  this.service.selectPageByHarm(new Paginator(request), entity) ;
		} catch (AppException e) {
			return BaseResult.loadFailed();
		}
	}

	@GetMapping(value = "/searchCarByNum")
	public Object searchCarByNum(String num) {
		try {
			return this.service.searchCarByNum(num);
		} catch (AppException e) {
			return BaseResult.loadFailed();
		}
	}
	
	/**
	 * 上传驾驶证
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/upload")
	public Object upload(HttpServletRequest request) {
		try {
			List<FileMeta> upload = FileUtil.upload(request) ;
			if(!upload.isEmpty()) {
				FileMeta fileMeta = upload.get(0);
				UploadResult result = this.uploadCloudStroe.uploadFile(new File(fileMeta.getDestFilePath())) ;
				if(result.getStatus()) {
					return BaseResult.build(true, "上传成功", result) ;
				}
			}
			
		} catch (Exception e) {
			return BaseResult.failure();
		}
		return BaseResult.ok();
	}

}
